using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Common;	
using SystemFrameworks;
using Microsoft.ApplicationBlocks.Data;

namespace MinCultura.Sipa.AgentesArtes
{
	/// <summary>
	/// Clase de acceso a datos para la entidad ART_AREA_ARTISTICAS
	/// </summary>
	public class AreaArtisticas
	{

        #region propiedades

        private string _cadenaConexion;

        public string ConnectionString
        {
            get { return _cadenaConexion; }
            set { _cadenaConexion = value; }
        }

        #endregion

		#region Private Data Access Atributes
		private const String ARD_ID_PARAM = "@ARD_ID";
		private const String ARD_AREA_ARTISTICA_PARAM = "@ARD_AREA_ARTISTICA";	         	    
		#endregion Private Data Access Atributes
	  

		/// <summary>
		/// Constructor de la clase ARTAREAARTISTICAS
		/// </summary>
        public AreaArtisticas(string strCadenaConecxion)
        {
            ConnectionString = strCadenaConecxion;
        }


		public MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData BuscarAreaArtisticas()
		{
		MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData response = new MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData();
		SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_AREA_ARTISTICAS", response.Tables[0]);
			if (response.Tables[0].Rows.Count == 0)
				return null;
			return response;
		}

		public DataTable BuscarAreaArtisticasEntidad()
		{
			DataTable response = new DataTable();
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_AREA_ARTISTICAS_ENTIDAD", response);
			if (response.Rows.Count == 0)
				return null;
			return response;
		}

		public DataTable BuscarAreaArtisticasComite()
		{
			DataTable response = new DataTable();
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_AREA_ARTISTICAS_COMITE", response);
			if (response.Rows.Count == 0)
				return null;
			return response;
		}
		
		
		public MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData ConsultarAreaArtisticas	(decimal parARD_ID)
		{
		MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData response = new MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData();
		
			SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, parARD_ID);
			
			SqlParameter[] parameters = {param1};		
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AREA_ARTISTICAS", response.Tables[0], parameters);
			if (response.Tables[0].Rows.Count == 0)
				return null;
			return response;
		}

			public DataTable ConsultarAreaArtisticasEntidades(decimal parARD_ID)
			{
				DataTable response = TablaAreasArtisticas();
		
				try
				{
					SqlParameter param1 = new SqlParameter("@ART_ID", parARD_ID);
			
					SqlParameter[] parameters = {param1};
					SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AREA_ARTISTICAS_ENTIDAD", response, parameters);
					if (response.Rows.Count == 0)
						return null;
					
				}
				catch
				{
					response = null;
				}

				return response;				
			}


			public MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData ConsultarAreaArtisticasComite(decimal parARD_ID)
			{
				MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData response = new MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData();
		
				try
				{
					SqlParameter param1 = new SqlParameter("@ARD_ID", parARD_ID);
			
					SqlParameter[] parameters = {param1};

					SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_AREA_ARTISTICAS_COMITE", response.Tables[0], parameters);

					if (response.Tables[0].Rows.Count == 0)
						return null;					
				}
				catch
				{
					response = null;
				}

				return response;				
			}

			public decimal  AdicionarAreaArtisticasEntidad (string strART_NOMBRE)	
			{
				
				decimal decART_ID=0;
					
				try
				{
					//StartTransaction();

					SqlParameter param1 = new SqlParameter("@ART_ID", SqlDbType.Decimal);
					param1.Direction=ParameterDirection.Output;
					SqlParameter param2 = new SqlParameter("@ART_NOMBRE", strART_NOMBRE);					
					SqlParameter[] parameters = {param1,param2};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AREA_ARTISTICAS_ENTIDAD", parameters); 				
					decART_ID=decimal.Parse(param1.Value.ToString());
					
                    //if (this.RegistroAuditoria!=null)
                    //{
                    //    this.RegistroAuditoria.Rows[0][RegistroAuditoriaData.REG_IDENTIFICADOR_FIELD] = decART_ID;
                    //}

                    //RegistrarAuditoria();

                    //CompleteTransaction();

					return(decART_ID);				 
				} 		
				catch (SqlException sqlEx)
				{		
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", "ART_AREA_ARTISTICAS"),ex); 
				}	
			}		

			public decimal  AdicionarAreaArtisticasComite(string strART_NOMBRE)	
			{				
				decimal decART_ID=0;
					
				try
				{
					//StartTransaction();
		
					SqlParameter param1 = new SqlParameter("@ARD_ID", SqlDbType.Decimal);
					param1.Direction=ParameterDirection.Output;
					SqlParameter param2 = new SqlParameter("@ARD_AREA_ARTISTICA", strART_NOMBRE);					
					SqlParameter[] parameters = {param1,param2};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AREA_ARTISTICAS_COMITE", parameters); 				
					decART_ID=decimal.Parse(param1.Value.ToString());
					
                    //if (this.RegistroAuditoria!=null)
                    //{
                    //    this.RegistroAuditoria.Rows[0][RegistroAuditoriaData.REG_IDENTIFICADOR_FIELD] = decART_ID;
                    //}

                    //RegistrarAuditoria();

                    //CompleteTransaction();

					return(decART_ID);									 
				} 		
				catch (SqlException sqlEx)
				{	
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", "ART_AREA_ARTISTICAS"),ex); 
				}	
			}		

			public decimal  AdicionarAreaArtisticas (MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData datos)	
			{				
				DataTable tabla = datos.Tables[MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ART_AREA_ARTISTICAS_TABLE];
				decimal decIMU_ID=0;

				try
				{
					//StartTransaction();

					foreach (DataRow row in tabla.Rows)
					{
						SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, SqlDbType.Decimal);
						param1.Direction=ParameterDirection.Output;
						SqlParameter param2 = new SqlParameter(ARD_AREA_ARTISTICA_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ARD_AREA_ARTISTICA_FIELD]);
					
						SqlParameter[] parameters = {param1,param2};
						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_AREA_ARTISTICAS", parameters); 
						decIMU_ID=decimal.Parse(param1.Value.ToString());
					}

                    //if (this.RegistroAuditoria!=null)
                    //{
                    //    this.RegistroAuditoria.Rows[0][RegistroAuditoriaData.REG_IDENTIFICADOR_FIELD] = decIMU_ID;
                    //}

                    //RegistrarAuditoria();

                    //CompleteTransaction();
				} 
				catch (SqlException sqlEx)
				{	
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "ART_AREA_ARTISTICAS"),ex); 
				}

				return(decIMU_ID);
			}		

		
		public void ActualizarAreaArtisticas (MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData datos)
		{
			DataTable tabla = datos.Tables[MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ART_AREA_ARTISTICAS_TABLE];
			MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData areaArtisticasDataAnt = new MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData();

			try
			{
                //if (this.RegistroAuditoria!=null)
                //{
                //    areaArtisticasDataAnt = ConsultarAreaArtisticas(Convert.ToDecimal(tabla.Rows[0][MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ARD_ID_FIELD]));	
                //}

                //StartTransaction();

				foreach (DataRow row in tabla.Rows)
				{				
					SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ARD_ID_FIELD]);
					SqlParameter param2 = new SqlParameter(ARD_AREA_ARTISTICA_PARAM, row[MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ARD_AREA_ARTISTICA_FIELD]);
						
					SqlParameter[] parameters = {param1,param2};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ACTUALIZAR_ART_AREA_ARTISTICAS", parameters); 
				}
 
                //RegistrarAuditoria(areaArtisticasDataAnt.Tables[0],tabla);

                //CompleteTransaction();
			} 
			catch (SqlException sqlEx)
			{	
				//AbortTransaction();
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
			}     
			catch (Exception ex) 
			{ 
				//AbortTransaction();
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "ART_AREA_ARTISTICAS"),ex); 
			}
		} 

			public void ActualizarAreaArtisticasEntidad (DataTable tabla)
			{
				DataTable tablaAnt = TablaAreasArtisticas();
				try
				{
                    //if (this.RegistroAuditoria!=null)
                    //{						
                    //    tablaAnt = ConsultarAreaArtisticasEntidades(Convert.ToDecimal(tabla.Rows[0]["ART_ID"]));
                    //}

					foreach (DataRow row in tabla.Rows)
					{				
						SqlParameter param1 = new SqlParameter("@ART_ID", row["ART_ID"]);
						SqlParameter param2 = new SqlParameter("@ART_NOMBRE", row["ART_NOMBRE"]);
					
						SqlParameter[] parameters = {param1,param2};

						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ACTUALIZAR_ART_AREA_ARTISTICAS_ENTIDAD", parameters); 
					}
					
					//RegistrarAuditoria(tablaAnt,tabla);
				} 
				catch (SqlException sqlEx)
				{		
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "ART_AREA_ARTISTICAS"),ex); 
				}
			} 

			public void ActualizarAreaArtisticasComite (MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData datos)
			{
				DataTable tabla = datos.Tables[0];
				MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData areasDataAnt = new MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData();
				
				try
				{
                    //StartTransaction();

                    //if (this.RegistroAuditoria!=null)
                    //{
                    //    areasDataAnt = ConsultarAreaArtisticasComite(Convert.ToDecimal(tabla.Rows[0][MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ARD_ID_FIELD]));
                    //}

					foreach (DataRow row in tabla.Rows)
					{				
						SqlParameter param1 = new SqlParameter("@ARD_ID", row[MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ARD_ID_FIELD]);
						SqlParameter param2 = new SqlParameter("@ARD_AREA_ARTISTICA", row[MinCultura.Sipa.DataAccess.AgentesArtes.AreaArtisticasData.ARD_AREA_ARTISTICA_FIELD]);
					
						SqlParameter[] parameters = {param1,param2};
						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ACTUALIZAR_ART_AREA_ARTISTICAS_COMITE", parameters); 
					}

                    //RegistrarAuditoria(areasDataAnt.Tables[0],tabla);
 
                    //CompleteTransaction();
				} 
				catch (SqlException sqlEx)
				{	
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "ART_AREA_ARTISTICAS"),ex); 
				}
			} 
		
			/// <summary>
			/// Elimina un registro de la entidad Artareaartisticas
			/// </summary>
			/// <param name="parARD_ID">Identificador del registro
			public void EliminarAreaArtisticas (decimal parARD_ID)
			{
				try
				{
					SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, parARD_ID);
						
					SqlParameter[] parameters = {param1};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREA_ARTISTICAS", parameters); 
				}
				catch (SqlException sqlEx)
				{		
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREA_ARTISTICAS"),ex); 
				}
			}
			
			public void EliminarAreaArtisticas(ArrayList arrIDs)
			{				
				try
				{
					//StartTransaction();

					foreach(object Item in arrIDs)
					{		
						SqlParameter param1 = new SqlParameter(ARD_ID_PARAM, decimal.Parse(Item.ToString()));
						SqlParameter[] parameters = {param1};
						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREA_ARTISTICAS", parameters); 
					}

					//RegistrarAuditoria();

					//CompleteTransaction();
				}
				catch (SqlException sqlEx)
				{
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}
				catch (Exception ex)
				{
					//AbortTransaction();
					throw new ApplicationExceptionManager (Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREA_ARTISTICAS"),ex); 
				}				  					 
			}
			
			public void EliminarAreaArtisticasEntidad (decimal parARD_ID)
			{
				try
				{
					SqlParameter param1 = new SqlParameter("@ART_ID", parARD_ID);
						
					SqlParameter[] parameters = {param1};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREA_ARTISTICAS_ENTIDAD", parameters); 
				}
				catch (SqlException sqlEx)
				{		
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREAS_ARTISTICAS_ENTIDAD"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREAS_ARTISTICAS_ENTIDAD"),ex); 
				}
			}
			
			public void EliminarAreaArtisticasEntidad(ArrayList arrIDs)
			{
				try
				{
					//StartTransaction();

					foreach(object Item in arrIDs)
					{		
						SqlParameter param1 = new SqlParameter("@ART_ID", decimal.Parse(Item.ToString()));
						SqlParameter[] parameters = {param1};
						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREA_ARTISTICAS_ENTIDAD", parameters); 
					}

					//RegistrarAuditoria();

					//CompleteTransaction();
				}
				catch (SqlException sqlEx)
				{
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}
				catch (Exception ex)
				{
					//AbortTransaction();
					throw new ApplicationExceptionManager (Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREA_ARTISTICAS"),ex); 
				}				  					 

			}

			//AREAS ARTISTICAS COMITE
			public void EliminarAreaArtisticasComite(decimal parARD_ID)
			{
				try
				{
					SqlParameter param1 = new SqlParameter("@ARD_ID", parARD_ID);
						
					SqlParameter[] parameters = {param1};
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREA_ARTISTICAS_COMITE", parameters); 
				}
				catch (SqlException sqlEx)
				{		
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREAS_ARTISTICAS_ENTIDAD"),sqlEx); 
				}     
				catch (Exception ex) 
				{ 
					throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREAS_ARTISTICAS_ENTIDAD"),ex); 
				}
			}	

			public void EliminarAreaArtisticasComite(ArrayList arrIDs)
			{
				try
				{
					//StartTransaction();

					foreach(object Item in arrIDs)
					{		
						SqlParameter param1 = new SqlParameter("@ARD_ID", decimal.Parse(Item.ToString()));
						SqlParameter[] parameters = {param1};

						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_AREA_ARTISTICAS_COMITE", parameters); 
					}

					//RegistrarAuditoria();

					//CompleteTransaction();
				}
				catch (SqlException sqlEx)
				{
					//AbortTransaction();
					throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx,"ART_AREA_ARTISTICAS"),sqlEx); 
				}
				catch (Exception ex)
				{
					//AbortTransaction();
					throw new ApplicationExceptionManager (Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_AREA_ARTISTICAS"),ex); 
				}
			}
			
		public DataTable TablaAreasArtisticas()
		{
			DataTable dtAreasArtisticas = new DataTable();

			dtAreasArtisticas.Columns.Add("ART_ID",typeof(System.Decimal));
			dtAreasArtisticas.Columns["ART_ID"].Caption = "Codigo";
			dtAreasArtisticas.Columns.Add("ART_NOMBRE",typeof(System.String));
			dtAreasArtisticas.Columns["ART_NOMBRE"].Caption = "Nombre";

			return dtAreasArtisticas;
		}

	}	
}		

